package com.example.memosystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.memosystem.MainActivity;
import com.example.memosystem.R;
import com.example.memosystem.dao.impl.AdminDao;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        EditText account = findViewById(R.id.register_count);
        EditText password = findViewById(R.id.register_pwd);
        EditText name = findViewById(R.id.register_name);
        RadioButton man = findViewById(R.id.register_man);
        RadioButton woman = findViewById(R.id.register_woman);
        man.setChecked(true);
        EditText phone = findViewById(R.id.register_phone);
        EditText age = findViewById(R.id.register_age);
        Button button = findViewById(R.id.register_botton);
        //返回登录界面
        Button login=findViewById(R.id.register_quit);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String res_account = account.getText().toString().trim();
                String res_password = password.getText().toString().trim();
                String res_name = name.getText().toString().trim();
                String res_phone = phone.getText().toString().trim();
                String res_age = age.getText().toString().trim();

                if(res_account.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                }else if(res_password.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else if(res_name.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                }else if(res_phone.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入电话", Toast.LENGTH_SHORT).show();
                }else if(res_age.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入年龄", Toast.LENGTH_SHORT).show();
                }else {
                    String sex = "女";
                    if(man.isChecked())
                    {
                        sex = "男";
                    }
                    AdminDao adminDao = new AdminDao();
                    int a = adminDao.registerAdmin(res_account, res_password, res_name, sex, res_phone, res_age);
                    if(a == 0)
                    {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this, "登录界面", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });



    }
}